<?php
namespace Admin\Controller;
use Admin\Model\newsModel;
use Think\AjaxPage;
use Think\Page;
class ChatController extends BaseController {
    
    public function index(){
		$this->display();
	}
	
	public function ajaxIndex(){
		$condition = array();
		$user_id = I('user_id')?I('user_id'):'';
		$user_id && $condition['user_id'] = $user_id;
		$form_id = I('form_id')?I('form_id'):'';
		$form_id && $condition['form_id'] = $form_id;
        $sort_order = 'id desc';
        $model = M('users_chat');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count,20);
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
		$mod = M('users');
		foreach((array)$userList as $k=>$v){
			$userList[$k]['user_name'] = $mod->where(array('id'=>$v['user_id']))->getField('id');
			$userList[$k]['form_name'] = $mod->where(array('id'=>$v['form_id']))->getField('id');
		}
        $this->assign('userList', $userList);
        $this->assign('page', $show);
        $this->display();		
	}
	
	public function send(){
		
		$this->display();
	}
	public function ajaxSend(){
		$condition = array();
        $sort_order = 'id desc';
        $model = M('users_chat_log');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('page', $show);
        $this->display();
		
	}
	public function formSend(){
		$uid = M('config')->where(array('name'=>'contact'))->getField('value');
		if(!$uid){
			$this->error('请先设置客服ID');
		}else{
			$content = I('content')?I('content'):'';
			$type_id = I('type_id')?I('type_id'):'';
			$user_mod = M('users');
			$where['status'] = 1;
			$where['id'] = array('neq',$uid);
			if($type_id>0){			
				$where['type_id'] = $type_id;
			}
			$list = $user_mod->where($where)->select();
			$chat_mod = M('users_chat');
			$chat_list_mod = M('users_chat_list');
			$time = time();
			$user = '';
			foreach((array)$list as $k=>$v){				
				$check = $chat_list_mod->where(array('user_id'=>$uid,'form_id'=>$v['id']))->count();
				if($check){
					$chat_list_mod->where(array('form_id'=>$uid,'user_id'=>$v['id']))->save(array('status'=>1));
					$chat_list_mod->where(array('user_id'=>$uid,'form_id'=>$v['id']))->save(array('status'=>1));
				}else{
					$chat_list_mod->add(array('form_id'=>$uid,'user_id'=>$v['id'],'status'=>1,'add_time'=>$time));
					$chat_list_mod->add(array('user_id'=>$uid,'form_id'=>$v['id'],'status'=>1,'add_time'=>$time));
				}
				$chat_mod->add(array('user_id'=>$uid,'form_id'=>$v['id'],'type_id'=>0,'add_time'=>$time,'content'=>$content));
				$user.=$v['id'].',';
			}
			$result = M('users_chat_log')->add(array('user'=>rtrim($user,','),'content'=>$content,'add_time'=>$time));
			$this->success('发送成功', U('Chat/send'));
		}
	}
}
